package com.caits.lbs.framework.services.redis;

import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * RedisSentinelConfig
 * 使用哨兵配置JedisConnectionFactory，用于主备自动切换，客户端不需要修改
 * @author zhangzhiheng
 * @date 2017/4/11
 */
public class RedisSentinelConfig extends RedisSentinelConfiguration{
    private String sentinelStr;
    private String masterName;

    public RedisSentinelConfig(String sentinelStr, String masterName){
        this.sentinelStr = sentinelStr;
        this.masterName = masterName;
        List<RedisNode> sentinelNodes = new ArrayList<RedisNode>();
        String[] sts = sentinelStr.split(",");
        for(String s: sts) {
            RedisNode node = new RedisNode(s.split(":")[0],Integer.parseInt(s.split(":")[1]));
            sentinelNodes.add(node);
        }
        setMaster(masterName);
        setSentinels(sentinelNodes);
    }

    public String getSentinelStr() {
        return sentinelStr;
    }

    public void setSentinelStr(String sentinelStr) {
        this.sentinelStr = sentinelStr;
    }

    public String getMasterName() {
        return masterName;
    }

    public void setMasterName(String masterName) {
        this.masterName = masterName;
    }
}
